package com.author.server.service.controller;

import com.auth.service.api.Account;
import com.auth.service.api.AuthResponseResult;
import com.auth.service.api.MessageCode;
import com.author.server.service.JwtService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.UUID;

import static com.auth.service.api.MessageCode.SUCCESS_CODE;
import static com.auth.service.api.MessageCode.USERNAME_NOT_FOUND;

/**
 * @Author:RenPu
 * @Date: 2020/10/28 21:07
 * @Version: 1.0
 * @description:
 */

@RestController
public class JwtController {


    @Resource
    private JwtService jwtService;


    @Resource
    private RedisTemplate redisTemplate;

    @PostMapping("login")
    public AuthResponseResult login(
            @RequestParam("username") String username
            , @RequestParam("password") String password) {

        Account account =new Account();
        account.setUsername(username);


        // TODO  在此处需要进行与数据进行校验此用户的存在性

        String token = jwtService.getToken(account);

        //将生成的token进行封装
        account.setToken(token);
        //生成备用的token
        account.setRefreshtoken(UUID.randomUUID().toString());

        //将备用的token，作为key  account作为对象进行保存到redis
        redisTemplate.opsForValue().set(account.getRefreshtoken(), account);


        AuthResponseResult result = new AuthResponseResult(account,SUCCESS_CODE);


        return result;
    }


    @PostMapping("refresh")
    public AuthResponseResult refresh(@RequestParam("refreshToken")
                                              String refreshToken) {
        //检验refreshToken是否在redis中存在
        Account account = (Account) redisTemplate.opsForValue().get(refreshToken);

        if (account == null) {
            AuthResponseResult result = new AuthResponseResult();
            result.setCode(USERNAME_NOT_FOUND);
            return result;

        }

        //重新生成新的token
        String token = jwtService.getToken(account);

        //将新的token以及备用token进行封装到实体
        account.setToken(token);
        account.setRefreshtoken(UUID.randomUUID().toString());

        //移除redis的老的备用token
        redisTemplate.delete(refreshToken);

        //将新生成的备用token作为key,account作为value作为值，进行存储到redis
        redisTemplate.opsForValue().set(account.getRefreshtoken(), account);

        AuthResponseResult  result=new AuthResponseResult(account,SUCCESS_CODE);
        return result;

    }


    @GetMapping("verify")
    public AuthResponseResult verify(@RequestParam("username") String username,
                                     @RequestParam("token") String token) {

        boolean verify = jwtService.verify(username, token);

        AuthResponseResult result= new  AuthResponseResult ();
        //TODO 此处最好使用 invalid token之类的信息错误
        result.setCode(verify ? SUCCESS_CODE : USERNAME_NOT_FOUND);
        return result;

    }


}
